package com.Ajava1704.for线程池;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @类名: com.Ajava1704.for线程池
 * @作者: lgt
 * @创建时间: 2020-02-11 19:32
 * @描述:
 **/
public class ForXianChengchiTest {

    public static void main(String[] args) {

        LinkedBlockingQueue<Runnable> linkedBlockingQueue = new LinkedBlockingQueue<>();
        ForRejectHandler forRejectHandler = new ForRejectHandler(linkedBlockingQueue);
        ArrayBlockingQueue<Runnable> arrayBlockingQueue = new ArrayBlockingQueue<>(2);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2, //核心线程数
                4, //最大线程数
                50,  //线程的存活时间
                TimeUnit.SECONDS,  //线程存活时间的单位
                arrayBlockingQueue,  //工作队列
                forRejectHandler
        );


        for (int i = 0; i < 7; i++) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("====这是执行的业务代码===");
                }
            };
            threadPoolExecutor.execute(runnable);
        }
        System.out.println(linkedBlockingQueue.size());
    }

}
